﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SaleOnlineSystem.Controls
{
    public partial class BoxEmailGetNewProduct : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            txtEmailCustomer.Attributes.Add("onkeypress", "return clickButton(event,'" + btnSendMailCustomer.ClientID + "')");
        }

        protected void btnSendMailCustomer_Click(object sender, EventArgs e)
        {
            int nextid = Convert.ToInt32(DAL.DCollector.DBCollector.Instance.getNextID("tbl_Bank", "Bank_ID"));
            string emailcustomer = txtEmailCustomer.Text;
            if (LIB.Validate.checkEmail(emailcustomer) == true)
            {
                DataTable dt = DAL.DCollector.DBCollector.Instance.CallStored("callstored", "Select count(*) as result from tbl_EmailCustomer where EmailCustomer_Email='" + emailcustomer+"'", true);
                int count = Convert.ToInt32(dt.Rows[0]["result"].ToString());
                if (count != 0)
                {
                    if (nextid == 0)
                    {
                        string value = "1,'" + emailcustomer + "'";
                        List<string> values = new List<string>();
                        values.Add(value);
                        DAL.DCollector.DBCollector.Instance.StoredProcedureInsertByRows("tbl_EmailCustomer", "insertemployee", values, false);
                        Response.Redirect("/trangchu.htm");
                        lblCheckEmail.Text = "";
                    }
                    else
                    {
                        int idnextmail = nextid + 1;
                        string value = "" + idnextmail + ",'" + emailcustomer + "'";
                        List<string> values = new List<string>();
                        values.Add(value);
                        DAL.DCollector.DBCollector.Instance.StoredProcedureInsertByRows("tbl_EmailCustomer", "insertemployee", values, false);
                        Response.Redirect("/trangchu.htm");
                        lblCheckEmail.Text = "";
                    }
                    
                }
                else
                {
                    lblCheckEmail.Text = "Email này đã tồn tại";
                }
            }
            else
            {
                lblCheckEmail.Text = "Email không hợp lệ";
            }
        }
    }
}